package com.droidnova.android.games.vortex;

import java.io.BufferedReader;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.microedition.khronos.opengles.GL10;



public class points extends Application{
	
	
	
	
	
	public static final String LOG_TAG = "Droidnova";
    
	//private int _noVertices = 8;   //no of points to be drawn
    private int _noVertices = 0;
	//variables
	float g=0.0f,a=0.5f,b=0.5f,c=0.5f;
	private float[] _pvertices ,_intensity ;
	private String _new[] ,_even[],_odd[] , _points[];
	float max_value=0.0f;
	public float max_xvalue , max_yvalue , max_zvalue;
	public float max_xvalue1 , max_yvalue1 , max_zvalue1;
	
	// Our index buffer.
	//private ShortBuffer _pindexBuffer;
	
	//private short[] _pindices = {0,1,1,2,2,3,3,0,4,5,5,6,6,7,7,4,3,7,2,6,0,4,1,5};  // The order we like to connect them.
	//private short[] _pindices = {0,1,1,2,2,3,3,0,4,5,5,6,6,7,7,4,7,3,6,2,4,2,1,5};  // The order we like to connect them.
    // FileInputStream fIn = null;
	
	/*private static points instance;
    static Context context2;
	public static points getInstance() {
	   if(instance == null){
	      instance = new points(context2);
	   }
	   return instance;
	 }
	*/
	
    
		
	/*private float[] _pvertices = { 
	        g, g, g,      //front bottom left  v1
	        a, g, g,     //front  bottom right  v2
	        a, b, g,     //front  top   right   v3
	        g, b, g,     //front  top  left     v4 
	        g, g, c,    // back bottom left   v5
	        a, g, c,    // back bottom right v6
	        a, b, c,    // back top right  v7
	        g, b, c,    // back top left   v8
	       
	       	        
	     };*/
	float cr = 0.9f,cg=0.3f ,ca=1.0f;
	 /** The initial color definition */	
	/*private float _pcolors[] = {
			            0.0f,  1.0f,  0.0f,  1.0f,
			            0.0f,  1.0f,  0.0f,  1.0f,
			            1.0f,  0.5f,  0.0f,  1.0f,
			            1.0f,  0.5f,  0.0f,  1.0f,
			            1.0f,  0.0f,  0.0f,  1.0f,
			            1.0f,  0.0f,  0.0f,  1.0f,
			            0.0f,  0.0f,  1.0f,  1.0f,
			            1.0f,  0.0f,  1.0f,  1.0f
			    			};*/
	
	
	/*private float _pcolors[] = {
			cr,  cg,  0.1f,  1.0f,
			cr,  cg,  0.2f,  1.0f,
			cr,  cg,  0.3f,  1.0f,
			cr,  cg,  0.5f,  1.0f,
			cr,  cg,  0.6f,  1.0f,
			cr,  cg,  0.7f,  1.0f,
			cr,  cg,  0.87f,  1.0f,
			cr,  cg,  1.0f,  1.0f
    			};*/
	// Our vertex buffer.
	private FloatBuffer _pvertexBuffer;
	//our Color buffer
	private FloatBuffer _pcolorBuffer;
	
	public points(Context ctx)
	{
		//this.ctx = ctx;  //NEW

Log.w("MyClassName", "This is a warning");

		
		InputStream fIn = ctx.getResources().openRawResource(R.raw.input);
		if(fIn==null)
		{
			Log.w("Nullerror", "null");
		}
		else
		{
		
  InputStreamReader isr = new InputStreamReader(fIn);
  BufferedReader br = new BufferedReader(isr);
  String stringRead = null;
  
  ArrayList <String> words = new ArrayList<String> ();
  ArrayList <String> points = new ArrayList<String>();
  
  ArrayList<String> even = new ArrayList<String>();
  ArrayList<String> odd = new ArrayList<String>();
  
	
  
try {
		stringRead = br.readLine( );
} catch (IOException e) {
	
		e.printStackTrace();
}
  while( stringRead != null ) // end of the file?
  {
		// process the line read
	  	

		StringTokenizer st = new StringTokenizer(stringRead,",");
		
		while(st.hasMoreTokens())
		{
		    words.add(st.nextToken());
		 
		}
		//while(st.hasMoreTokens())
		//{
	 // buf.append(stringRead + "\n" );
			//String[] RowData = stringRead.split(" ");
			//float data[] = Float.parseFloat(RowData);
			
			//String xv = RowData[0];
			//String yv = RowData[1];
			//String zv = RowData[3];
		//	float xv1 = Float.parseFloat(xv);
		//	float yv1 = Float.parseFloat(yv);
		//	float zv1 = Float.parseFloat(zv);*/
			
			//xv = Float.parseFloat( st.nextToken( ) );
			//yv = Float.parseFloat( st.nextToken( ) );
			//zv = Float.parseFloat( st.nextToken( ) );
			//float[] RowData = Float.parseFloat( st.nextToken( ) );
			//Log.w("FileOutput1", xv.toString());
			//Log.w("FileOutput2", yv.toString());
			//Log.w("FileOutput3", zv.toString());
			//Log.w("FileOutput", buf.toString());
			//Log.w("FileOutput1", xv);
			//Log.w("FileOutput2", yv);
			//Log.w("FileOutput3", zv);
			//Log.w("FileOutput3", RowData.toString());
		
		
			
		//}
  //}
			try {
				stringRead = br.readLine( );
			} catch (IOException e) {
				
				e.printStackTrace();
			}
			_new = words.toArray(new String[words.size()]);
			
			
			
			
			
			
			
  }
  
  
  for(int i=0;i<_new.length;i+=2)
	{
	even.add(_new[i]);	
	}
	
	for(int i=1;i<_new.length;i+=2)
	{
		odd.add(_new[i]);	
	}
  _even = even.toArray(new String[even.size()]);
  _odd = odd.toArray(new String[odd.size()]);
  
  // To test if _new ( contains all points), EVEN(contains all x,y,z points) and ODD (contains intensity values) work
/*  for (String op : _new)
  {
      Log.v("ALL", "FOUND string -> " + op);
  }
  for (String ev : _even)
  {
      Log.v("EVEN", "FOUND string -> " + ev);
  }
  for (String od : _odd)
  {
      Log.v("ODD", "FOUND string -> " + od);
  }*/
  
  for(int i=0 ;i < _even.length;i++)
  {
	  StringTokenizer st = new StringTokenizer(_even[i], " "); 
	  while(st.hasMoreTokens())
		{
		    points.add(st.nextToken()+ "f");
		  
		}
  }
  _points = points.toArray(new String[points.size()]);
  try{
	_pvertices = convertStringArraytoFloatArray(_points);
	_intensity = convertStringArraytoFloatArray(_odd);
} catch (Exception e) {
	
	e.printStackTrace();
	
}

//to test if _pvertices and _intensity works properly
  /*for (Float op1 : _pvertices)
  {
      Log.v("best float", "FOUND float -> " + op1);
  }
  for (Float in : _intensity)
  {
      Log.v("best intensity", "FOUND float -> " + in);
  }*/

	
	}
		
		
		
		_noVertices = _intensity.length ;
		int number = _intensity.length *4;
	
		 Log.v("NUMBER", "FOUND NUMBER -> " + number);
		float _pcolors[] = new float[number];
		//calculate color values for each point
		int count=0;
	    float i_value,cb = 0.0f;
		max_value = getMaxValue(_intensity);
		for(int i = 0;i<_intensity.length;i++)
		{		
		i_value = _intensity[i];
		cb = i_value/max_value;
			for(int j=0;j<=3;j++)
			{
				if(j==0)
				{
					_pcolors[count]=cr;	
					// Log.v("best color", "FOUND color -> " + _pcolors[count]);
				}
				else if(j==1)
				{
					_pcolors[count]=cg;	
					// Log.v("best color", "FOUND color -> " + _pcolors[count]);
				}
				else if(j==2)
				{
					_pcolors[count]=cb;
					// Log.v("best color", "FOUND color -> " + _pcolors[count]);
				}
				else
				{
					_pcolors[count]=ca;	
					 //Log.v("best color", "FOUND color -> " + _pcolors[count]);
				}
				count++;
				// Log.v("count", "FOUND count -> " + count);
				//if(count== _pcolors.length-1)
				//{
				//	break;
				//}
			}
		
		}
		
		/*for (Float pc : _pcolors)
		  {
		      Log.v("best color", "FOUND float -> " + pc);
		  }*/
		
		// a float is 4 bytes, therefore we multiply the number if 
		// vertices with 4.
		ByteBuffer vbb = ByteBuffer.allocateDirect(_pvertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		_pvertexBuffer = vbb.asFloatBuffer();
		_pvertexBuffer.put(_pvertices);
		_pvertexBuffer.position(0);	
		
		//color buffer
		ByteBuffer cbb = ByteBuffer.allocateDirect(_pcolors.length * 4);
		cbb.order(ByteOrder.nativeOrder());
		_pcolorBuffer = cbb.asFloatBuffer();
		_pcolorBuffer.put(_pcolors);
		_pcolorBuffer.position(0);
		
	
		
		//calculate the highest x  point
		for(int i =0;i<_pvertices.length;i+=3)
		{
			max_xvalue = getMaxValue(_pvertices);
			
		}
		setMax_xvalue(max_xvalue);
		//calculate the highest y point
		for(int i =0;i<_pvertices.length;i+=3)
		{
			max_yvalue = getMaxValue(_pvertices);
			
		}
		setMax_yvalue(max_yvalue);
		//calculate the hightest z point
		for(int i =0;i<_pvertices.length;i+=3)
		{
			max_zvalue = getMaxValue(_pvertices);
			
		}
		setMax_zvalue(max_zvalue);
		
		
		}               //END OF POINTS CONTRUCTOR >>>>>>>>
	
	
	
	// Getters and Setters for maximum xvalue, yvalue and zvalue
	public float getMax_xvalue() {
		return max_xvalue;
	}

	public void setMax_xvalue(float max_xvalue) {
		max_xvalue1 = max_xvalue;
		Log.v("X V", "X V -> " + max_xvalue1);
	}

	public float getMax_yvalue() {
		return max_yvalue;

	}

	public void setMax_yvalue(float max_yvalue) {
		max_yvalue1 = max_yvalue;
		Log.v("Y V", "Y V -> " + max_yvalue1);
	}

	public float getMax_zvalue() {
		return max_zvalue;
	}

	public void setMax_zvalue(float max_zvalue) {
		max_zvalue1 = max_zvalue;
		Log.v("Z V", "Z V -> " + max_zvalue1);
	}
	
	
	

	//conversion ofstring array to float array	
	public float[] convertStringArraytoFloatArray(String[] _new) throws Exception 
	{
		if (_new != null)
		{
		float floatarray[] = new float[_new.length];
		for (int i = 0; i < _new.length; i++) {
		floatarray[i] = Float.parseFloat(_new[i]);
		}
		return floatarray;
		}
		return null;
		}
	// Finding the maximum value in an array
	public float getMaxValue(float[] numbers){  
		  float maxValue = numbers[0];  
		  for(int i=1;i < numbers.length;i++){  
		    if(numbers[i] > maxValue){  
		      maxValue = numbers[i];
		    }  
		  }  
		  return maxValue;  
		}  
	
   
	
	public void draw(GL10 gl) {
		
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		// Specifies the location and data format of an array of vertex
		// coordinates to use when rendering.
		 // set the color of our element
        // set the color of our element
		
	    // define the vertices we want to draw
	    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _pvertexBuffer);
	    gl.glColorPointer(4, GL10.GL_FLOAT, 0, _pcolorBuffer);
	    //set the point size
	    gl.glPointSize(8);
        // finally draw the vertices
	    gl.glRotatef(45.0f, 0.0f,0.0f,1.0f);
	    gl.glDrawArrays(GL10.GL_POINTS, 0, _noVertices);
		// Disable the vertices buffer.
	    
	    
	  //  gl.glLineWidth(6);
	  //  gl.glRotatef(45.0f, 0.0f,0.0f,1.0f);
	  //  gl.glDrawElements(GL10.GL_LINES, _pindices.length, GL10.GL_UNSIGNED_SHORT, _pindexBuffer);
	    
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
	}
}
